<template>
  <div class="app-container">
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="650px" @close="resetForm('ruleForm')">
      <button-group ref="buttonGroup" @deleteCancel="deleteCancel" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
      <div class="formTopFont">检验项信息</div>
      <el-row>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="检验项名称" prop="itemName">
                <el-input v-model="ruleForm.itemName" maxlength="20" show-word-limit />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="检验项编号" prop="itemCode">
                <el-input v-model="ruleForm.itemCode" maxlength="20" show-word-limit />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="检验类别" prop="classifyId">
                <tree-select ref="treeSelect" style="width: 100%" :props="{
                    value: 'id',
                    label: 'classifyName',
                    children: 'children'
                  }" :options="treeData" :disabled="false" :accordion="true" :value="ruleForm.classifyId" search-key="classifyName" @getNode="getValue($event)" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否启用" prop="status">
                <el-select v-model="ruleForm.status" clearable>
                  <el-option :value="1" label="启用"></el-option>
                  <el-option :value="0" label="停用"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" type="textarea" rows="3" maxlength="500" show-word-limit />
            </el-form-item>
          </el-row>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/qm/check/item/verifyNameUnique?itemName=${value}&id=${this.ruleForm.id}`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('检验项名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        id: '',
        itemName: '',
        itemCode: '',
        classifyId: '',
        remark: ''
      },
      rules: {
        itemName: [
          { required: true, message: '检验项名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        itemCode: [
          { required: true, message: '检验项编号不能为空！', trigger: 'blur' }
        ],
        classifyId: [
          { required: true, message: '请选择检验类别！', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      treeData: [],
      obj: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    showDialog(row, data) {
      this.obj = row
      this.dialogVisible = true
      this.treeData = [...data]
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取选中的上级部门
     * @param {*} value
     * @return {*}
     */
    getValue(value) {
      if (value) {
        this.ruleForm.classifyId = value.id
        this.ruleForm.classifyName = value.classifyName
      } else {
        this.ruleForm.classifyId = ''
        this.ruleForm.classifyName = ''
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/qm/check/item/submit',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    async deleteCancel() {
      if (this.title === '编辑') {
        this.$confirm(`确认删除数据？`, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios
              .delete(`/qm/check/item/remove/${this.obj.id}`)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$notify.success('删除成功！')
                  this.dialogVisible = false
                  this.$emit('getTableInfo', 1)
                } else {
                  this.$notify.error(res.data.msg)
                }
              })
          })
          .catch(() => {})
      }
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        id: '',
        itemName: '',
        itemCode: '',
        classifyId: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
